package com.sita.tboard.hitchhike.history;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.hitchhike.bean.TicketModel;
import com.sita.tboard.util.DateUtils;
import com.sita.tboard.util.NumberUtils;

import java.util.ArrayList;
import java.util.List;

public class DriverHistoryTicketsAdapter extends RecyclerView.Adapter<DriverHistoryTicketsAdapter.ViewHolder> {

    private ArrayList<TicketModel> mValues = new ArrayList<TicketModel>();

    private Context mContext;

    public DriverHistoryTicketsAdapter(Context context) {
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hitchhike_driver_history, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TicketModel item = mValues.get(position);
        holder.bindData(item);

    }

    public TicketModel getItem(int position) {
        return mValues.get(position);
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public void setData(List<TicketModel> items){
        mValues.clear();
        mValues.addAll(items);
        notifyItemRangeChanged(0, items.size());
    }

    public void append(List<TicketModel> items) {
        int count = getItemCount();
        mValues.addAll(items);
        if (items.size() == 1) {
            notifyItemInserted(count);
        } else if (items.size() > 1) {
            notifyItemRangeInserted(count, items.size());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTimeView;
        private TextView mFromView;
        private TextView mToView;
        private TextView mMoneyView;

        public ViewHolder(View view) {
            super(view);

            mTimeView = (TextView) view.findViewById(R.id.time);
            mFromView = (TextView) view.findViewById(R.id.from);
            mToView = (TextView) view.findViewById(R.id.to);
            mMoneyView = (TextView) view.findViewById(R.id.money);
        }

        public void bindData(TicketModel item) {
            mTimeView.setText(GlobalContext.getGlobalContext().getString(R.string.departure_time, DateUtils.formatDateTime(item.orderDepartureTime)));
            mFromView.setText(GlobalContext.getGlobalContext().getString(R.string.from, item.fromaddress));
            mToView.setText(GlobalContext.getGlobalContext().getString(R.string.to, item.toaddress));
            if (item.money != null) {
                mMoneyView.setText(GlobalContext.getGlobalContext().getString(R.string.payment_rmb,
                        NumberUtils.formatCurrency(item.money)));
            }
        }
    }

}
